package com.lepu.app.fun.grow.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.EmojiFilter;
import com.core.lib.utils.main.UIHelper;
import com.eyzhs.app.R;
import com.lepu.app.main.activity.MainTabActivity;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class GrowSettingAlbumNameActivity extends BaseFragmentActivity
		implements OnTopbarNewLeftLayoutListener,
		OnTopbarNewRightButtonListener, AsyncRequest,OnEditorActionListener {

	@Override
	@SuppressLint("InflateParams")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grow_setting_name_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("设置相册名字");
		topbar.setRightText("保存");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);

		EditText contentEditText = (EditText) findViewById(R.id.contentEditText);
		contentEditText.setFilters(new InputFilter[] { new EmojiFilter(),new InputFilter.LengthFilter(20) });
		contentEditText.setOnEditorActionListener(this);
		/*
		 * contentEditText.setText(AppConfig.getConfigString(this,
		 * Const.CONFIG_APP_ALBUM_NAME, Const.DEFAULT_CUSTOM_ALBUM_NAME));
		 */
		contentEditText.setText(UserConfig.getConfigString(this,
				Const.CONFIG_APP_ALBUM_NAME, Const.DEFAULT_CUSTOM_ALBUM_NAME));

	}

	private void saveContent() {
		
		boolean flag = MainTabActivity.jumpLogin(this);
		if(flag)
		{
			return;
		}
		
		EditText contentEditText = (EditText) findViewById(R.id.contentEditText);
		String content = contentEditText.getText().toString().trim();
		if (TextUtils.isEmpty(content)) {
			UIHelper.showToast(this, "请输入相册名称");
			return;
		}

		UserConfig.setConfig(this, Const.CONFIG_APP_ALBUM_NAME, content);

		if (MyApplication.getInstance().checkIsLogin()) {
			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			String url = Setting.getModiyAlbumNameUrl();

			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);
				params.put("UserID", loginInfo.UserID);
				params.put("LoginToken", loginInfo.LoginToken);

				params.put("AlbumName", content);
			} catch (Exception e) {
			}
			map.put("ht", params);

			showProgressDialog();
			ApiClient.http_post_main(url, map, null, this, "", false);
		}
	}

	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		
		saveContent();
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		hideProgressDialog();
		finish(true);
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		hideProgressDialog();
		finish(true);
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		
		switch (actionId) {
		case EditorInfo.IME_ACTION_SEND:
			
			saveContent();
			break;

		default:
			break;
		}
		return true;
	}
	
	
}
